﻿using Excel2DbTool.Common;
using Excel2DbTool.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Excel2DbTool.Helpers
{
    public sealed class DbSysDictManager
    {
        public readonly FWXZDict FWXZDict;
        public readonly FWLXDict FWLXDict;
        public readonly FWYTDict FWYTDict;
        public readonly FWJGDict FWJGDict;
        public readonly HXDict HXDict;
        public readonly HXJGDict HXJGDict;
        public readonly BLZTDict BLZTDict;

        private DbSysDictManager()
        {
            FWXZDict = new FWXZDict();
            FWLXDict = new FWLXDict();
            FWYTDict = new FWYTDict();
            FWJGDict = new FWJGDict();

            HXDict = new HXDict();
            HXJGDict = new HXJGDict();
            BLZTDict = new BLZTDict();
        }

        static DbSysDictManager manager;
        public static DbSysDictManager Instance
        {
            get
            {
                return manager ?? (manager = new DbSysDictManager());
            }
        }
    }


    public abstract class DbDict
    {
        static readonly string SqlTemplate = "SELECT SD_KEYNO,SD_VALUE FROM OA2_SYSDIC WHERE SD_CLASS={0} AND SD_MAIN<>0 ORDER BY SD_oid";

        protected IList<DbDictTableItem> dict = new List<DbDictTableItem>(10);

        public DbDict(int type)
        {
            var datatable = GetDictByType(type);
            FillDict(dict, datatable);
        }

        //根据Val获取对应的key
        public virtual string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);
                return dbDictTableItem.Key;
            }
        }

        DataTable GetDictByType(int type)
        {
            string sql = string.Format(SqlTemplate, type);
            var datatable = SqlHelper.GetInstance().Select(sql);
            return datatable;
        }

        void FillDict(IList<DbDictTableItem> dict, DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DbDictTableItem item = new DbDictTableItem();

                item.Key = dt.Rows[i][0].ToString();
                item.Val = dt.Rows[i][1].ToString();

                dict.Add(item);
            }
        }
    }

    /// <summary>
    /// 房屋性质 16
    /// </summary>
    public class FWXZDict : DbDict
    {
        public FWXZDict()
            : base(16)
        {

        }
    }

    /// <summary>
    /// 房屋结构 17
    /// </summary>
    public class FWJGDict : DbDict
    {
        public FWJGDict()
            : base(17)
        {

        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "6";
                }
                return dbDictTableItem.Key;
            }
        }
    }

    /// <summary>
    /// 房屋类型 97
    /// </summary>
    public class FWLXDict : DbDict
    {
        public FWLXDict()
            : base(97)
        {

        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "99";
                }
                return dbDictTableItem.Key;
            }
        }
    }

    /// <summary>
    /// 房屋用途,19
    /// </summary>
    public class FWYTDict : DbDict
    {
        public FWYTDict()
            : base(19)
        {
        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "80";
                }
                return dbDictTableItem.Key;
            }
        }
    }

    //户型
    public class HXDict : DbDict
    {
        public HXDict()
            : base(99)
        {

        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "99";
                }
                return dbDictTableItem.Key;
            }
        }
    }

    //户型结构 100
    public class HXJGDict : DbDict
    {
        public HXJGDict()
            : base(100)
        {

        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {

                    if (val.Contains(item.Val) || item.Val.Contains(val))
                    {
                        return true;
                    }
                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "99";
                }
                return dbDictTableItem.Key;
            }
        }
    }

    //办理状态: 130 对应:CQZT[产权状态],DYZT[抵押状态],CFZT[查封状态],BAZT[备案状态]
    public class BLZTDict : DbDict
    {
        public BLZTDict()
            : base(130)
        {
        }

        public override string this[string val]
        {
            get
            {
                Func<DbDictTableItem, bool> func = (item) =>
                {
                    if (item.Val.Length <= 0 || val.Length <= 0)
                    {
                        return false;
                    }

                    if (val.Substring(1).Equals(item.Val.Substring(1)))
                    {
                        if (val.Substring(0, 1).Equals("已"))
                        {
                            return true;
                        }
                    }

                    return false;
                };

                var dbDictTableItem = this.dict.FirstOrDefault(func);

                if (dbDictTableItem == null)
                {
                    return "0";
                }
                return dbDictTableItem.Key;
            }
        }
    }


}
